package pikes.peak;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.web.servlet.support.RequestContext;

import pikes.html.xhtml.Body;
import pikes.html.xhtml.Head;
import pikes.html.xhtml.XHTMLPage;

/**
 * PikesPeakPage renders a strict, valid and (optionally) formatted XHTML page to the 
 * http response without manipulating directly html tags. 
 * Subclasses should override {@link #buildPage(Page)} method and change the properties of the Page.
 * For example:
 * <br/>
 * <blockquote>
 * <pre>
 * @Override
 * protected void buildPage(Page page) {
 * 	page.setTitle(new RequestProperty<CharSequence>() {
 * 
 * 		public CharSequence getValue(Map model, RequestContext requestContext) throws Exception {
 * 			return new Date().toString();
 *  		}	
 *  		
 * 	});
 * }
 * </pre>
 * </blockquote>
 * will render a strict, valid XHTML page whose title will be the current time. 
 *
 */
public class PikesPeakPage extends XHTMLPage implements InitializingBean {

	private Page page = new Page();

	public PikesPeakPage() {
		super();
		// buildPage(page);
	}

	@Override
	protected final CharSequence getTitle(Map model, HttpServletRequest request) throws Exception {
		return page.getTitle().getValue(model, new RequestContext(request));
	}

	@Override
	protected final void buildBody(Body body, Map model, HttpServletRequest request) throws Exception {
		page.buildBody(body, model, new RequestContext(request));

	}

	@Override
	protected void buildHeader(Head head, Map model, HttpServletRequest request)
			throws Exception {
		page.buildHeader(head, model, new RequestContext(request));

	}

	protected void buildPage(Page page) {
		// left empty for empty page
	}

	protected void translate() {

	}

	public final void afterPropertiesSet() throws Exception {
		translate();
		buildPage(page);

	}
}
